package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// DeptController 部门管理
type DeptController struct {
	Ctx iris.Context
}

// GetList 列出所有部门
func (c *DeptController) GetList() mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:query") {
		return nil
	}
	deptName := c.Ctx.URLParam("deptName")
	status := c.Ctx.URLParam("status")
	list, err := service.DeptService.GetList(deptName, status)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(list)
}

// Post 添加部门
func (c *DeptController) Post(req vo.AddDeptReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:add") {
		return nil
	}
	if !service.DeptService.CheckDeptNameUnique(0, req.DeptName) {
		return action.Error("新增部门'" + req.DeptName + "'失败，部门名称已存在")
	}
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err := service.DeptService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// GetBy 根据id获取
func (c *DeptController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:query") {
		return nil
	}
	dept, err := service.DeptService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(dept)
}

// Put 修改部门
func (c *DeptController) Put(req vo.EditDeptReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:edit") {
		return nil
	}
	if !service.DeptService.CheckDeptNameUnique(req.DeptID, req.DeptName) {
		return action.Error("修改部门'" + req.DeptName + "'失败，部门名称已存在")
	}
	if req.DeptID == req.ParentID {
		return action.Error("修改部门'" + req.DeptName + "'失败，上级部门不能是自己")
	}
	if req.Status == "1" {
		ids, err := service.DeptService.GetChild(req.DeptID)
		if err != nil {
			systemLog.Errorln(err.Error())
			return action.ServerError()
		}
		if len(ids) > 0 {
			return action.Error("该部门包含未停用的子部门！")
		}
	}
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err := service.DeptService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// DeleteBy 删除部门
func (c *DeptController) DeleteBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:remove") {
		return nil
	}
	if service.DeptService.HasChildByDeptId(id) {
		return action.Error("存在下级部门,不允许删除")
	}
	if service.DeptService.CheckDeptExistUser(id) {
		return action.Error("部门存在用户,不允许删除")
	}

	err := service.DeptService.Delete(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// GetListExcludeBy 查询部门列表 排除节点
func (c *DeptController) GetListExcludeBy(deptId int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:dept:list") {
		return nil
	}
	list, err := service.DeptService.GetList("", "")
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	for index, item := range list {
		if item.DeptID == deptId {
			list = append(list[:index], list[index+1:]...)
			break
		}
	}
	return action.Data(list)
}
